<?php

App::uses('AppModel', 'Model');

/**
 * Building Model
 *
 */
class Building extends AppModel {

    public $primaryKey = 'Id';

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'name';

    /**
     * Validation rules
     *
     * @var array
     */
     public $belongsTo = array(
        'Customer' => array(
            'className' => 'Customers',
            'foreignKey' => 'CustId'
        )
    );
    public $validate = array(
        'BuildId' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => '物件IDを入力してください。',
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'Unique' => array(
                'rule' => 'isUnique',
                'message' => 'この物件IDが登録されています。',
            ),
            'lenght' => array(
                'rule' => array('maxLength', 20),
                'message' => '20桁以下で入力して下さい。'
            ),
            'single_bye'=>array(
                'rule'=>'/^[\w\d\s.,-]*$/',
                'message'=>'英数字で入力してください。'
            ),
        ),
        'BuildName' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => '物件名を入力してください。',
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'lenght' => array(
                'rule' => array('detecedUtf8Character', 100),
                'message' => '100桁以下で入力して下さい。'
            )
        ),
        'BuildAddress' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => '物件住所を入力してください。',
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'lenght' => array(
                'rule' => array('detecedUtf8Character', 200),
                'message' => '200桁以下で入力して下さい。'
            )
        ),
        'BuildPhone' => array(
            'phone' => array(
                'rule' => '/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                'message' => '電話番号は数字の9桁以上で入力してください。',
                'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'lenght' => array(
                'rule' => array('maxLength', 20),
                'message' => '20桁以下で入力して下さい。'
            )
        ),
        'CustId' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'お客様を選択してください。',
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
    public function beforeSave($options = array()) {
        parent::beforeSave($options);
        foreach ($this->data[$this->alias] as $key => $value) {
            $this->data[$this->alias][$key]=  trim($value);
        }
        return true;
    }
    /**
     * 
     */
    

    /**
     * 
     */
    public function detecedUtf8Character($param,$lenght){
        $value=  array_values($param);
        if(strlen(utf8_decode($value['0']))>$lenght)
            return false;
        return true;
    }
    public function getAllBuilding($condition = array()) {
        $user = CakeSession::read("Auth.User");
        if (!$user['IsAdmin']) {
            $condition['Building.CustId'] = $user['Id'];
        }
        if (count($condition) > 0) {
            $return = $this->find('all', array('conditions' => $condition));
        } else {
            $return = $this->find('all');
        }
        return $return;
    }

    /**
     * 
     */
    public function getListBuilding() {
        $user = CakeSession::read("Auth.User");
        $condition = array();
        if (!$user['IsAdmin']) {
            $condition['Building.CustId'] = $user['Id'];
        }
        if (count($condition) > 0) {
            $data = $this->find('list', array('conditions' => $condition, 'fields' => array('Id', 'BuildName')));
        } else {
            $data = $this->find('list', array('fields' => array('Id', 'BuildName')));
        }
        return $data;
    }

    /**
     * 
     */
    public function getListBuildingOfCustomer($customerId) {
        $data = $this->find('list', array('conditions' => array('CustId' => $customerId), 'fields' => array('Building.Id', 'Building.BuildName')));
        return $data;
    }

    /**
     * 
     */
    public function beforeDelete($cascade = true) {
        parent::beforeDelete($cascade);
        App::import('model','Camera');
        $ob = new Camera();
        $ob->deleteAll("Camera.BuildId = '".$this->id."'");
        return true;
    }
   

}
